<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="../js/utils.js"></script>
</head>
<body>
  <h1>注册</h1>
  <form>
    <p>用户名：<input type="text" id="name"></p>
    <p>密码：<input type="password" id="pwd"></p>
    <button>注册</button>
  </form>
  <script>
    var form = document.querySelector('form')
    var nameInput = document.querySelector('#name')
    var pwdInput = document.querySelector('#pwd')
    form.onsubmit = function () {
      const name = nameInput.value
      const pwd = pwdInput.value
      // 带上用户名和密码向后端发送请求
      // { name: name, pwd: pwd } 在ES6里可以简写为{ name, pwd }
      utils.post('../api/user/regist.php', { name, pwd }, resp => {
        if (resp.code === 200) {
          alert(resp.body.msg + '，即将跳转登录页')
          location.href = './login.html'
        } else {
          // 注册失败
          alert(resp.body.msg)
        }
      })
      return false
    }
  </script>
</body>
</html>